package com.jeffers.videorecord;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public final class StorageUtils {
	
	private StorageUtils(){};
	
	public static String initialCacheDirectory(Context context,String dir){
		String path = null;
		try {
			if(context.getExternalCacheDir() == null) {
				String cacheDir = "/Android/data/" + context.getPackageName() + "/cache";
				 new File(Environment.getExternalStorageDirectory().getPath() + cacheDir).mkdirs();
			}
			String externalDirString = context.getExternalCacheDir().getPath();
			path = externalDirString+ File.separator+dir;
			File file = new File(path);
			if(!file.exists()){
				file.mkdir();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return path;
	}
	
	public static String getCacheDirectory(Context context,String dir){
		if(context.getExternalCacheDir()!=null){
			return context.getExternalCacheDir().getPath()+ File.separator+dir;
		}
		return null;
	}
	
	//默认下载目录
	public static String getExternalDownloadDirectory(){
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
			return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
		}
		return null;
	}
	
	public static File createFile(String dir,File dirFile,String fileName){
		File file = null;
		try {
			if(TextUtils.isEmpty(fileName)){
				return null;
			}
			if(!TextUtils.isEmpty(dir)){
				file = new File(dir, fileName);
			}else if(dirFile!=null){
				file = new File(dirFile, fileName);
			}
			if(file!=null&&!file.exists()){
				file.createNewFile();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return file;
	}
	
	public static void deleteFile(File file){
		if(file==null){
			return;
		}
		if(file.isDirectory()&&file.listFiles()!=null){
			File[] childFiles = file.listFiles();
			for (File child : childFiles) {
				deleteFile(child);
			}
		}
		file.delete();
	}
	
	public static void deleteFile(String path){
		if(TextUtils.isEmpty(path)){
			return;
		}
		File file = new File(path);
		deleteFile(file);
	}
	public static void clearDirectory(File file){
		if(file==null){
			return;
		}
		if(file.isDirectory()&&file.listFiles()!=null){
			File[] childFiles = file.listFiles();
			for (File child : childFiles) {
				deleteFile(child);
			}
		}
	}

	public static boolean copyFileByNIOChannel(File src,File des){
		FileInputStream fis = null;
		FileOutputStream fos = null;
		FileChannel in = null;
		FileChannel out = null;
		try {
			fis = new FileInputStream(src);
			fos = new FileOutputStream(des);
			in = fis.getChannel();
			out = fos.getChannel();
			in.transferTo(0, in.size(), out);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			if(out != null){
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(in != null){
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fos != null){
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fis != null) {
				try{
					fis.close();
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
		return true;
	}


}
